<?php
namespace Home\Controller;
class GoodsController extends CommonController {
    public function index($id){
	
		$goods=D('goods');
		//当前商品的所有会员价格
		$levelPrice=$goods->levelprice($id);
		//当前商品当前登录会员的商品价格
		$mprice=$goods->getMemberPrice($id);
		$goodsInfo=$goods->alias('a')->field('a.*,b.id,b.brand_name,b.brand_url')->join('LEFT JOIN sp_brand b ON a.brand_id=b.id')->where(array('a.id'=>$id))->find();
		$gp=D('goodsPic');
		$gpres=$gp->where(array('goods_id'=>$id))->select();
		$cate=D('cate');
		$topId=$cate->getparent($goodsInfo['cate_id']);//找到当前商品的顶级栏目
		$navRes=$cate->getnav($topId);
		$gattr=D('goodsAttr');
		$lis= $gattr->field('a.*,b.attr_name,b.attr_type')->alias('a')->join('LEFT JOIN sp_attr b ON a.attr_id=b.id')->where(array('goods_id'=>$id))->select();
		$radioAttr=array();
		$uniAttr=array();
		foreach($lis as $k=>$v){
			if($v['attr_type']==1){
				$radioAttr[$v['attr_id']][]=$v;
			}else{
				$uniAttr[]=$v;
			}
		}

		$this->assign(array(
			'goodsInfo'=>$goodsInfo,
			'gpres'=>$gpres,
			'radioAttr'=>$radioAttr,
			'uniAttr'=>$uniAttr,
			'navRes'=>$navRes,
			'levelPrice'=>$levelPrice,//当前商品的所有会员价格
			'mprice'=>$mprice,//当前商品当前登录会员的商品价格
			'goodsId'=>$id,
		));
		
	$this->display();
    }
	
	//获取商品库存量
	public function getgoodsnum($gid,$gaid=''){
		$goods = D("goods");
		echo $goods->getGoodsNum($gid,$gaid);
	}
}